<?php
namespace app\mobile\Controller;
use think\captcha\Captcha;
use think\Controller;

class Login extends Controller
{
	private $table_admin = '';

	/*
	*	构造函数
	*/
	function __construct(){
		parent::__construct();
		$this->table_admin = db("member");
	}
	/*
    *	管理员登陆页面
    */
	public function login()
	{
		return $this->fetch();
	}

	/*
	*	登陆处理
	*/
	public function login_do(){
		$account = input("post.account");
//		$mobile  = input("post.mobile");
		$passwd  = input("post.passwd");
		if($account==""){
			$this->error("账号不能为空");
		}
		if($passwd==""){
			$this->error("密码不能为空");
		}

		$data = $this->table_admin ->where("username = '$account'")->find();

		$verifyCode = input("verifyCode");
		$res = $this->check_verify($verifyCode);

		if($res!=true)$this->error("验证码不正确！");
		if(empty($data)) $this->error("账号不存在");

		if($data['passwd'] != md5($passwd)){
			$this->error("密码错误！");
		}elseif($data['status']==0){
			$this->error("该账号被禁止使用!");
		}else{
			session("info_user",$data);
			//sessionid更新
			$session_id=session_id();
			$this->update_session_id($data['id'], $session_id);
			$l['loginip']   = get_client_ip();
			$l['logintime'] = time();
			$this->table_admin ->where("username='$account'")->update($l);
			$this->success('登录成功','Index/index');
		}

	}

	/**
	* 注册
	*/
	public function zregister()
	{
		return $this->fetch();
	}

	public function register()
	{
		$account = input("post.account");
		$account3 = input("post.account3");
		$company = input("post.company");
		$mobile = input("post.mobile");
		$passwd  = input("post.passwd");

		if($account==""){
			$this->error("账号不能为空");
		}
		if($account3==""){
			$this->error("姓名不能为空");
		}
		if($passwd==""){
			$this->error("密码不能为空");
		}
		$data = $this->table_admin->where("username='$account'")->find();

		if(!empty($data)){
			$this->error("该账号名已注册");
		}

		$verifyCode = input("verifyCode");
		$res = $this->check_verify($verifyCode);
		if($res!=true)  $this->error("验证码不正确！");

		$web_set_result = db('web_set')->field('type')->find();
		$member_authority_result = db('member_authority')->where(array('id'=>$web_set_result['type']))->field('status')->find();

		if(empty($web_set_result['type']) || empty($member_authority_result) || $member_authority_result['status'] == '-1'){
			$this->error("还没有开放注册！");
		}


		$data['username'] = $account;
		$data['name'] = $account3;
		$data['passwd'] = md5($passwd);
		$data['company'] = $company;
		$data['mobile'] = $mobile;
		$data['create_time'] = time();
		$data['type'] = $web_set_result['type'];

		$result = db('member')->insert($data);
		if(!$result)$this->error("注册失败");

		//权限赋予
		$uid = db('member')->getLastInsID();
		$data['id'] = $uid;

		//用户权限更新
		user_input($uid, $web_set_result['type']);

		session("info_user",$data);
		//sessionid更新
		$session_id=session_id();
		$this->update_session_id($uid, $session_id);
		$this->success('注册成功', url('Index/index'));
	}

	/*
	*	个人中推出登录
	*/
	public function login_out(){
		session("info_user",NULL);
		$this->success("欢迎再次使用!",'Index/index');
	}

	public function captchaImg()
	{	
		ob_clean();
		$config =    [
			'codeSet'	  =>'1234567890',
		// 验证码字体大小
			'fontSize'    =>    30,
		// 验证码位数
			'length'      =>    4,
		// 关闭验证码杂点
			'useNoise'    =>    true,
	];
		$captcha = new Captcha($config);
		return $captcha->entry();
	}
	/**
	 * 检测输入的验证码是否正确
	 *
	 * @param $code 为用户输入的验证码字符串，
	 * @param $id 多个验证码标识
	 *
	 */
	function check_verify($code, $id = ''){
		$captcha = new Captcha();
		return $captcha->check($code, $id);
	}

	/**
	*	检测是否登录
	*/
	function is_login(){
		$info_user = session("info_user");

		if(empty($info_user)){
			$this->error('未登录',url('Login/login'));
		}else{
			$this->success('已登录');
		}
	}

	/*
	*	检测是否是同一个窗口登录，如不是则清除该窗口登录信息并且更新登录状态
	*/
	function check_login(){
		session_start();
		$info_user = session("info_user");
		$session_id = session_id();
		if(empty($info_user))return $this->success();
		$id = $info_user['id'];
		$member = db('member')->where(['id'=>$id])->find();

		if(empty($member['session_id'])){
			return $this->success();
		}else{
			if($member['session_id'] == $session_id){
				return $this->success();
			}else{
				//其他设备登录账号并且更新session_id
				session('info_user',NULL);
				return $this->error('你已被挤下线!',url('Index/index'));
			}
		}
	}

	/*
	*	更新session_id实现单点登录
	*/
	function update_session_id($uid, $session_id){
		if(empty($uid) || empty($session_id))return false;
		db('member')->where(['id'=>$uid])->update(['session_id'=>$session_id]);
	}
}